/**
 * Copyright (c) 2023, iDearHanYu Co., Ltd.
 *
 * SPDX-License-Identifier: 
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-08-18     chao         first version
 */

#include <lvgl.h>
#include "volumio_music.h"
#include <string.h>

LV_IMG_DECLARE(img_volumio_music_btn_loop);
LV_IMG_DECLARE(img_volumio_music_btn_prev);
LV_IMG_DECLARE(img_volumio_music_btn_play);
LV_IMG_DECLARE(img_volumio_music_btn_pause);
LV_IMG_DECLARE(img_volumio_music_btn_next);
LV_IMG_DECLARE(img_volumio_music_btn_rnd);
LV_IMG_DECLARE(img_volumio_music_slider_knob);

/* private variables */
static const lv_font_t *player_font_small;
static const lv_font_t *player_font_large;
static lv_obj_t *player_main_cont;
static lv_obj_t *player_source_label;
static lv_obj_t *player_title_label;
static lv_obj_t *player_artist_label;
static lv_obj_t *player_album_label;
static lv_obj_t *player_button_prev;
static lv_obj_t *player_button_next;
static lv_obj_t *player_button_play_pause;
static lv_obj_t *player_slider_obj;
static lv_obj_t *player_time_label;
static lv_obj_t *player_image;
static uint32_t LV_EVENT_USER;
static uint8_t user_param;

/* private functions */
static void _player_key_event_cb(lv_event_t * e);
static void _player_button_event_cb(lv_event_t * e);
static void _player_user_event_cb(lv_event_t * e);
static lv_obj_t *_create_player_cont(lv_obj_t *parent);
static void _create_wave_images(lv_obj_t *parent);
static lv_obj_t *_create_player_source_box(lv_obj_t *parent);
static lv_obj_t *_create_player_title_box(lv_obj_t *parent);
static lv_obj_t *_create_player_ctrl_box(lv_obj_t *parent);
static lv_obj_t *_create_player_image_box(lv_obj_t *parent);

/* extern functions */
extern int lv_indev_get_user_button(lv_point_t *point);

lv_obj_t *volumio_music_player_create(void)
{
    player_font_small = &lv_font_montserrat_22;
    player_font_large = &lv_font_montserrat_32;

    /* create the content of the music player */
    lv_obj_t *cont = _create_player_cont(lv_scr_act());
    _create_wave_images(cont);
    lv_obj_t *source_box = _create_player_source_box(cont);
    lv_obj_t *title_box = _create_player_title_box(cont);
    lv_obj_t *ctrl_box = _create_player_ctrl_box(cont);
    lv_obj_t *pic_box = _create_player_image_box(cont);

    static const lv_coord_t grid_cols[] = {LV_GRID_FR(2), LV_GRID_FR(1), 30, LV_GRID_TEMPLATE_LAST};
    static const lv_coord_t grid_rows[] = {
        40,              /* source box */
        LV_GRID_FR(1),
        LV_GRID_CONTENT, /* title box */
        LV_GRID_FR(1),
        LV_GRID_CONTENT, /* control box */
        LV_GRID_FR(1),
        20,
        LV_GRID_TEMPLATE_LAST
    };
    lv_obj_set_grid_dsc_array(cont, grid_cols, grid_rows);
    lv_obj_set_style_grid_row_align(cont, LV_GRID_ALIGN_SPACE_BETWEEN, 0);
    lv_obj_set_grid_cell(source_box, LV_GRID_ALIGN_STRETCH, 0, 2, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_set_grid_cell(title_box, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 2, 1);
    lv_obj_set_grid_cell(ctrl_box, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 4, 1);
    lv_obj_set_grid_cell(pic_box, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_STRETCH, 1, 5);
    
	LV_EVENT_USER = lv_event_register_id();
    lv_obj_add_event_cb(player_main_cont, _player_user_event_cb, LV_EVENT_USER, NULL);
    lv_obj_add_event_cb(player_main_cont, _player_button_event_cb, LV_EVENT_PRESSED, NULL);
    lv_obj_add_event_cb(player_main_cont, _player_button_event_cb, LV_EVENT_LONG_PRESSED, NULL);
    lv_obj_add_event_cb(player_main_cont, _player_button_event_cb, LV_EVENT_LONG_PRESSED_REPEAT, NULL);
    lv_obj_add_event_cb(player_main_cont, _player_button_event_cb, LV_EVENT_RELEASED, NULL);

    return cont;
}

void volumio_music_player_destroy(void)
{
    lv_obj_del(player_main_cont);
}

/* create music player container */
static lv_obj_t *_create_player_cont(lv_obj_t *parent)
{
    player_main_cont = lv_obj_create(parent);
    lv_obj_remove_style_all(player_main_cont);
    lv_obj_set_size(player_main_cont, lv_pct(100), lv_pct(100));

    lv_obj_t *cont = lv_obj_create(player_main_cont);
    lv_obj_remove_style_all(cont);
    lv_obj_set_size(cont, LV_HOR_RES - 1, lv_pct(100));
    lv_obj_set_x(cont, 1);
    lv_obj_set_style_bg_opa(cont, LV_OPA_100, 0);
    lv_obj_set_style_bg_color(cont, lv_color_hex(0xffffff), 0);

    return cont;
}

static void _create_wave_images(lv_obj_t *parent)
{
    LV_IMG_DECLARE(img_volumio_music_wave_top);
    LV_IMG_DECLARE(img_volumio_music_wave_bottom);
    lv_obj_t *wave_top = lv_img_create(parent);
    lv_img_set_src(wave_top, &img_volumio_music_wave_top);
    lv_obj_set_width(wave_top, LV_HOR_RES);
    lv_obj_align(wave_top, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_add_flag(wave_top, LV_OBJ_FLAG_IGNORE_LAYOUT);

    lv_obj_t *wave_bottom = lv_img_create(parent);
    lv_img_set_src(wave_bottom, &img_volumio_music_wave_bottom);
    lv_obj_set_width(wave_bottom, LV_HOR_RES);
    lv_obj_align(wave_bottom, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_add_flag(wave_bottom, LV_OBJ_FLAG_IGNORE_LAYOUT);
}

static lv_obj_t *_create_player_source_box(lv_obj_t *parent)
{
    lv_obj_t *cont = lv_obj_create(parent);
    lv_obj_remove_style_all(cont);
    lv_obj_set_height(cont, LV_SIZE_CONTENT);
    
    player_source_label = lv_label_create(cont);
    lv_obj_set_style_text_font(player_source_label, player_font_large, 0);
    lv_obj_set_style_text_color(player_source_label, lv_color_hex(0x504d6d), 0);
    lv_label_set_text(player_source_label, music_player_source_string_get(music_player_current_source_get()));
    lv_obj_set_height(player_source_label, lv_font_get_line_height(player_font_large));
    lv_obj_set_align(player_source_label, LV_ALIGN_TOP_MID);

    return cont;
}

static lv_obj_t *_create_player_title_box(lv_obj_t *parent)
{
    lv_obj_t *cont = lv_obj_create(parent);
    lv_obj_remove_style_all(cont);
    lv_obj_set_height(cont, LV_SIZE_CONTENT);
    lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_pad_hor(cont, 20, 0);
    
    player_title_label = lv_label_create(cont);
    lv_obj_set_style_text_font(player_title_label, player_font_large, 0);
    lv_obj_set_style_text_color(player_title_label, lv_color_hex(0x504d6d), 0);
    lv_label_set_text(player_title_label, music_player_title_get());
    lv_obj_set_size(player_title_label, lv_pct(100), lv_font_get_line_height(player_font_large) * 3 / 2);
    lv_label_set_long_mode(player_title_label, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_set_style_text_align(player_title_label, LV_TEXT_ALIGN_CENTER, 0);

    player_artist_label = lv_label_create(cont);
    lv_obj_set_style_text_font(player_artist_label, player_font_small, 0);
    lv_obj_set_style_text_color(player_artist_label, lv_color_hex(0x504d6d), 0);
    lv_label_set_text(player_artist_label, music_player_artist_get());
    lv_obj_set_width(player_artist_label, lv_pct(100));
    lv_label_set_long_mode(player_artist_label, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_set_style_text_align(player_artist_label, LV_TEXT_ALIGN_CENTER, 0);

    player_album_label = lv_label_create(cont);
    lv_obj_set_style_text_font(player_album_label, player_font_small, 0);
    lv_obj_set_style_text_color(player_album_label, lv_color_hex(0x8a86b8), 0);
    lv_label_set_text(player_album_label, music_player_album_get());
    lv_obj_set_width(player_album_label, lv_pct(100));
    lv_label_set_long_mode(player_album_label, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_set_style_text_align(player_album_label, LV_TEXT_ALIGN_CENTER, 0);

    return cont;
}

static lv_obj_t *_create_player_ctrl_box(lv_obj_t *parent)
{
    lv_obj_t *cont = lv_obj_create(parent);
    lv_obj_remove_style_all(cont);
    lv_obj_set_height(cont, LV_SIZE_CONTENT);
    lv_obj_set_style_pad_bottom(cont, 17, 0);
    char time_buff[8] = {0};

    static const lv_coord_t grid_col[] = {LV_GRID_FR(2), LV_GRID_FR(3), LV_GRID_FR(5), LV_GRID_FR(5), LV_GRID_FR(5), LV_GRID_FR(3), LV_GRID_FR(2), LV_GRID_TEMPLATE_LAST};
    static const lv_coord_t grid_row[] = {LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST};
    lv_obj_set_grid_dsc_array(cont, grid_col, grid_row);
//    lv_obj_set_grid_align(cont, LV_GRID_ALIGN_SPACE_EVENLY, LV_GRID_ALIGN_CENTER);
    
    lv_obj_t *icon;
    icon = lv_img_create(cont);
    lv_img_set_src(icon, &img_volumio_music_btn_rnd);
    lv_obj_set_grid_cell(icon, LV_GRID_ALIGN_CENTER, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    
    static lv_style_t prev_next_press_style;
    lv_style_init(&prev_next_press_style);
    lv_style_set_img_recolor_opa(&prev_next_press_style, LV_OPA_30);
    lv_style_set_img_recolor(&prev_next_press_style, lv_color_black());
    
    player_button_prev = lv_img_create(cont);
    lv_img_set_src(player_button_prev, &img_volumio_music_btn_prev);
    lv_obj_set_grid_cell(player_button_prev, LV_GRID_ALIGN_CENTER, 2, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_group_add_obj(lv_group_get_default(), player_button_prev);
    lv_obj_add_flag(player_button_prev, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_style(player_button_prev, &prev_next_press_style, LV_STATE_PRESSED);
    
    player_button_play_pause = lv_img_create(cont);
    if (music_player_play_state_get())
    {
        lv_img_set_src(player_button_play_pause, &img_volumio_music_btn_play);
    }
    else
    {
        lv_img_set_src(player_button_play_pause, &img_volumio_music_btn_pause);
    }
    lv_obj_set_grid_cell(player_button_play_pause, LV_GRID_ALIGN_CENTER, 3, 1, LV_GRID_ALIGN_CENTER, 0, 1);

    player_button_next = lv_img_create(cont);
    lv_img_set_src(player_button_next, &img_volumio_music_btn_next);
    lv_obj_set_grid_cell(player_button_next, LV_GRID_ALIGN_CENTER, 4, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_add_flag(player_button_next, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_style(player_button_next, &prev_next_press_style, LV_STATE_PRESSED);
    
    icon = lv_img_create(cont);
    lv_img_set_src(icon, &img_volumio_music_btn_loop);
    lv_obj_set_grid_cell(icon, LV_GRID_ALIGN_CENTER, 5, 1, LV_GRID_ALIGN_CENTER, 0, 1);

    player_slider_obj = lv_slider_create(cont);
    lv_obj_set_style_anim_time(player_slider_obj, 100, 0);
    lv_obj_set_height(player_slider_obj, 8);
    lv_obj_set_grid_cell(player_slider_obj, LV_GRID_ALIGN_STRETCH, 1, 4, LV_GRID_ALIGN_CENTER, 1, 1);
    lv_slider_set_range(player_slider_obj, 0, music_player_whole_time_get());
    lv_group_remove_obj(player_slider_obj);

    lv_obj_set_style_bg_img_src(player_slider_obj, &img_volumio_music_slider_knob, LV_PART_KNOB);
    lv_obj_set_style_bg_opa(player_slider_obj, LV_OPA_TRANSP, LV_PART_KNOB);
    lv_obj_set_style_pad_all(player_slider_obj, 20, LV_PART_KNOB);
    lv_obj_set_style_bg_grad_dir(player_slider_obj, LV_GRAD_DIR_HOR, LV_PART_INDICATOR);
    lv_obj_set_style_bg_color(player_slider_obj, lv_color_hex(0x569af8), LV_PART_INDICATOR);
    lv_obj_set_style_bg_grad_color(player_slider_obj, lv_color_hex(0xa666f1), LV_PART_INDICATOR);
    lv_obj_set_style_outline_width(player_slider_obj, 0, 0);
    
    player_time_label = lv_label_create(cont);
    lv_obj_set_style_text_font(player_time_label, player_font_small, 0);
    lv_obj_set_style_text_color(player_time_label, lv_color_hex(0x8a86b8), 0);
    lv_snprintf(time_buff, sizeof(time_buff), "%d:%02d", music_player_play_time_get()/60, music_player_play_time_get()%60);
    lv_label_set_text(player_time_label, time_buff);
    lv_obj_set_grid_cell(player_time_label, LV_GRID_ALIGN_END, 5, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    
    return cont;
}

static lv_obj_t *_create_player_image_box(lv_obj_t *parent)
{
    lv_obj_t *cont = lv_obj_create(parent);
    lv_obj_remove_style_all(cont);
    lv_obj_set_style_radius(cont, LV_RADIUS_CIRCLE, 0);
    lv_obj_set_style_clip_corner(cont, true, 0);
//    lv_obj_set_style_align(cont, LV_ALIGN_CENTER, 0);

    LV_IMG_DECLARE(img_lv_demo_music_cover_1);
    LV_IMG_DECLARE(img_lv_demo_music_cover_2);
    LV_IMG_DECLARE(img_lv_demo_music_cover_3);
    player_image = lv_img_create(cont);
    if (music_player_current_source_get()==SOURCE_BT)
        lv_img_set_src(player_image, &img_lv_demo_music_cover_1);
    else if (music_player_current_source_get()==SOURCE_STREAMING)
        lv_img_set_src(player_image, &img_lv_demo_music_cover_2);
    else
        lv_img_set_src(player_image, &img_lv_demo_music_cover_3);
    lv_obj_align(player_image, LV_ALIGN_CENTER, 0, 0);

    return cont;
}

void volumio_music_player_update(enum _player_update update)
{
    if (volumio_music_current_page_id_get() == PAGE_ID_PLAYER)
    {
        user_param = update;
        lv_event_send(player_main_cont, LV_EVENT_USER, &user_param);
    }
}

static void _player_key_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj = lv_event_get_current_target(e);
    lv_key_t key;
    if (code == LV_EVENT_KEY)
    {
        key = lv_indev_get_key(lv_indev_get_act());
        LV_LOG_USER("key %d", key);
        switch (key)
        {                
            case LV_KEY_HOME:
                case LV_KEY_BACKSPACE:
                volumio_music_page_switch(PAGE_ID_PLAYER);
                break;
                                
//            case LV_USER_KEY_SOURCE:
//                volumio_music_page_switch(PAGE_ID_SOURCE);
//                break;
//                
//            case LV_USER_KEY_PREV:
////                music_player_play_prev_cb();
//                lv_group_focus_obj(obj);
//                lv_obj_add_state(obj, LV_STATE_PRESSED);
//                break;
//                
//            case LV_USER_KEY_NEXT:
//                music_player_play_next_cb();
//                break;
//                
//            case LV_USER_KEY_PLAY_PAUSE:
//                music_player_play_pause_cb();
//                break;
        }
    }
}

static void _player_user_event_cb(lv_event_t * e)
{
    uint8_t param;
    char time_buff[8] = {0};
    
    if (lv_event_get_code(e) == LV_EVENT_USER)
    {
        param = *((uint8_t *)lv_event_get_param(e));
        switch (param)
        {
            case PLAYER_UPDATE_TITLE:
                lv_label_set_text(player_title_label, music_player_title_get());
                break;
                
            case PLAYER_UPDATE_ARTIST:
                lv_label_set_text(player_artist_label, music_player_artist_get());
                break;
                
            case PLAYER_UPDATE_ALBUM:
                lv_label_set_text(player_album_label, music_player_album_get());
                break;
                
            case PLAYER_UPDATE_PLAY_PAUSE:
                if (music_player_play_state_get())
                {
                    lv_img_set_src(player_button_play_pause, &img_volumio_music_btn_play);
                }
                else
                {
                    lv_img_set_src(player_button_play_pause, &img_volumio_music_btn_pause);
                }
                break;
                
            case PLAYER_UPDATE_PLAY_TIME:
                lv_snprintf(time_buff, sizeof(time_buff), "%d:%02d", music_player_play_time_get()/60, music_player_play_time_get()%60);
                lv_label_set_text(player_time_label, time_buff);
                lv_slider_set_value(player_slider_obj, music_player_play_time_get(), LV_ANIM_ON);
                break;
                
            case PLAYER_UPDATE_WHOLE_TIME:
                lv_slider_set_range(player_slider_obj, 0, music_player_whole_time_get());
                break;
        }
    }
}

static void _player_button_event_cb(lv_event_t * e)
{
    lv_point_t point;
    lv_event_code_t code = lv_event_get_code(e);

    lv_indev_get_point(lv_event_get_indev(e), &point);
    LV_LOG_USER("point %d,%d", point.x, point.y);
    switch (lv_indev_get_user_button(&point))
    {
        case LV_USER_BTN_SOURCE:
            if (code == LV_EVENT_RELEASED)
            {
                volumio_music_page_switch(PAGE_ID_SOURCE);
            }
            break;
            
        case LV_USER_BTN_PREV:
            if (code == LV_EVENT_PRESSED)
            {
                lv_obj_add_state(player_button_prev, LV_STATE_PRESSED);
            }
            else if (code == LV_EVENT_LONG_PRESSED)
            {

            }
            else if (code == LV_EVENT_LONG_PRESSED_REPEAT)
            {

            }
            else if (code == LV_EVENT_RELEASED)
            {
                lv_obj_clear_state(player_button_prev, LV_STATE_PRESSED);
                music_player_play_prev_cb();
            }
            break;
            
        case LV_USER_BTN_NEXT:
            if (code == LV_EVENT_PRESSED)
            {
                lv_obj_add_state(player_button_next, LV_STATE_PRESSED);
            }
            else if (code == LV_EVENT_LONG_PRESSED)
            {

            }
            else if (code == LV_EVENT_LONG_PRESSED_REPEAT)
            {

            }
            else if (code == LV_EVENT_RELEASED)
            {
                lv_obj_clear_state(player_button_next, LV_STATE_PRESSED);
                music_player_play_next_cb();
            }
            break;
            
        case LV_USER_BTN_PLAY_PAUSE:
            if (code == LV_EVENT_RELEASED)
            {
                music_player_play_pause_cb();
            }
            break;

        default:
            break;
    }
}




